package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.domain.entity.SysRole;
import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.StringUtils;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.framework.web.service.TokenService;
import com.ticket.system.domain.YanchuEvaluate;
import com.ticket.system.service.IYanchuEvaluateService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 演出评价Controller
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/system/evaluate")
public class YanchuEvaluateController extends BaseController
{
    @Autowired
    private IYanchuEvaluateService yanchuEvaluateService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询演出评价列表
     */
    @PreAuthorize("@ss.hasPermi('system:evaluate:list')")
    @GetMapping("/list")
    @ApiOperation("管控台演出评价列表")
    public TableDataInfo list(HttpServletRequest request, YanchuEvaluate yanchuEvaluate)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<SysRole> roles = loginUser.getUser().getRoles();
        if(!roles.isEmpty()){
            List<SysRole> manages = roles.stream().filter(v -> v.getRoleName().equalsIgnoreCase("集团管理员")).collect(Collectors.toList());
            if(!manages.isEmpty()){
                yanchuEvaluate.setOperaId(null);
            }
        }

        startPage();
        List<YanchuEvaluate> list = yanchuEvaluateService.selectYanchuEvaluateList(yanchuEvaluate);
        return getDataTable(list);
    }

    /**
     * 导出演出评价列表
     */
    @PreAuthorize("@ss.hasPermi('system:evaluate:export')")
    @Log(title = "演出评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuEvaluate yanchuEvaluate)
    {
        List<YanchuEvaluate> list = yanchuEvaluateService.selectYanchuEvaluateList(yanchuEvaluate);
        ExcelUtil<YanchuEvaluate> util = new ExcelUtil<YanchuEvaluate>(YanchuEvaluate.class);
        util.exportExcel(response, list, "演出评价数据");
    }

    /**
     * 获取演出评价详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:evaluate:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("评价详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuEvaluateService.selectYanchuEvaluateById(id));
    }

    /**
     * 新增演出评价
     */
//    @PreAuthorize("@ss.hasPermi('system:evaluate:add')")
    @Log(title = "演出评价", businessType = BusinessType.INSERT)
    @ApiOperation("新增演出评价")
    @PostMapping
    public AjaxResult add(@RequestBody YanchuEvaluate yanchuEvaluate,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yanchuEvaluate.setCreator(loginUser.getUser().getUserId());
        return toAjax(yanchuEvaluateService.insertYanchuEvaluate(yanchuEvaluate));
    }

    /**
     * 修改演出评价
     */
    @Log(title = "演出评价", businessType = BusinessType.UPDATE)
    @ApiOperation("修改演出评价")
    @PutMapping
    public AjaxResult edit(@RequestBody YanchuEvaluate yanchuEvaluate,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yanchuEvaluate.setCreator(loginUser.getUser().getUserId());
        return toAjax(yanchuEvaluateService.updateYanchuEvaluate(yanchuEvaluate));
    }

    /**
     * 删除演出评价
     */
    @PreAuthorize("@ss.hasPermi('system:evaluate:remove')")
    @Log(title = "演出评价", businessType = BusinessType.DELETE)
    @ApiOperation("删除演出评价")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuEvaluateService.deleteYanchuEvaluateByIds(ids));
    }

    @ApiOperation("小程序端演出 评价列表")
    @ApiImplicitParam(name = "performId", value = "演出编码", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/evaluatePerform/forAllUser")
    public Map evaluatePerform(HttpServletRequest request, @RequestParam(name = "performId") Long performId)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);

        startPage();
        List<Map> list = yanchuEvaluateService.getEvaluatePerformById(performId,loginUser);
        Map map = new HashMap();
        map.put("evaluateList",getDataTable(list));

        return map;
    }


    /**
     * 修改精选
     */
    @ApiOperation("修改精选")
    @PreAuthorize("@ss.hasPermi('system:evaluate:edit')")
    @Log(title = "修改精选", businessType = BusinessType.UPDATE)
    @PostMapping("/updateExcellent")
    public AjaxResult updateExcellent(String ids, Integer isExcellent)
    {
        if(StringUtils.isBlank(ids) || null == isExcellent){
            return toAjax(0);
        }
        return toAjax(yanchuEvaluateService.updateExcellent(ids,isExcellent));
    }


}
